<!DOCTYPE html>
<html lang="zh" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>追加器 | Log4a</title>
    <meta name="description" content="优雅、易用的HarmonyOS日志库">
    <meta name="generator" content="VitePress v1.1.4">
    <link rel="preload stylesheet" href="/log4a-docs/assets/style.Bs0ybW4o.css" as="style">
    
    <script type="module" src="/log4a-docs/assets/app.CKVFL8lc.js"></script>
    <link rel="preload" href="/log4a-docs/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/log4a-docs/assets/chunks/framework.DcQ8S4Qz.js">
    <link rel="modulepreload" href="/log4a-docs/assets/chunks/theme.C-i07yX7.js">
    <link rel="modulepreload" href="/log4a-docs/assets/guide_appender.md.lJX8Kh1o.lean.js">
    <link rel="icon" type="image/png" sizes="16x16" href="log4a@1x.svg">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar has-sidebar top" data-v-ae24b3ad data-v-ccf7ddec><div class="wrapper" data-v-ccf7ddec><div class="container" data-v-ccf7ddec><div class="title" data-v-ccf7ddec><div class="VPNavBarTitle has-sidebar" data-v-ccf7ddec data-v-ab179fa1><a class="title" href="/log4a-docs/" data-v-ab179fa1><!--[--><!--]--><!--[--><img class="VPImage logo" src="/log4a-docs/log4a@1x.svg" alt data-v-8426fc1a><!--]--><span data-v-ab179fa1>Log4a</span><!--[--><!--]--></a></div></div><div class="content" data-v-ccf7ddec><div class="content-body" data-v-ccf7ddec><!--[--><!--]--><div class="VPNavBarSearch search" data-v-ccf7ddec><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-ccf7ddec data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/log4a-docs/guide/getting-started" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>指南</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/log4a-docs/reference/log-manager" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>参考</span><!--]--></a><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-b6c34ac9><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-b6c34ac9><span class="text" data-v-b6c34ac9><!----><span data-v-b6c34ac9>1.4.0</span><span class="vpi-chevron-down text-icon" data-v-b6c34ac9></span></span></button><div class="menu" data-v-b6c34ac9><div class="VPMenu" data-v-b6c34ac9 data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuLink" data-v-e7ea1737 data-v-43f1e123><a class="VPLink link vp-external-link-icon" href="https://gitee.com/ericple/log4a/blob/master/library/CHANGELOG.md" target="_blank" rel="noreferrer" data-v-43f1e123><!--[-->更新日志<!--]--></a></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-ccf7ddec data-v-e6aabb21><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-e6aabb21 data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-ccf7ddec data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/ericple/log4a" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><span class="vpi-social-github" /></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-ccf7ddec data-v-d0bd9dde data-v-b6c34ac9><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-b6c34ac9><span class="vpi-more-horizontal icon" data-v-b6c34ac9></span></button><div class="menu" data-v-b6c34ac9><div class="VPMenu" data-v-b6c34ac9 data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-d0bd9dde><div class="item appearance" data-v-d0bd9dde><p class="label" data-v-d0bd9dde>Appearance</p><div class="appearance-action" data-v-d0bd9dde><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-d0bd9dde data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div></div></div><div class="group" data-v-d0bd9dde><div class="item social-links" data-v-d0bd9dde><div class="VPSocialLinks social-links-list" data-v-d0bd9dde data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/ericple/log4a" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><span class="vpi-social-github" /></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-ccf7ddec data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-ccf7ddec><div class="divider-line" data-v-ccf7ddec></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-575e6a36><div class="curtain" data-v-575e6a36></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-575e6a36><span class="visually-hidden" id="sidebar-aria-label" data-v-575e6a36> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible has-active" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>简介</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/what-is-log4a" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>什么是Log4a?</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/getting-started" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>快速开始</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/appender" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>追加器</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>输出日志</h2><!----></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/parameter-priority" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>参数作用优先级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/log-with-worker" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>使用多线程输出</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/format" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>日志消息构建</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>进阶使用</h2><!----></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/custom-appender" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>自定义追加器</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/log-to-socket" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>输出到服务器</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/custom-layout" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>自定义布局</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/intercept-console" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>console拦截</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/guide/best-practice" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>最佳实践</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0" data-v-575e6a36 data-v-b8d55f3b><!----><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/log4a-docs/reference/log-manager" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>API参考</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" role="navigation" data-v-3f215769 data-v-269c27a6><div class="content" data-v-269c27a6><div class="outline-marker" data-v-269c27a6></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-269c27a6>On this page</div><ul class="VPDocOutlineItem root" data-v-269c27a6 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _log4a-docs_guide_appender" data-v-39a288b8><div><h1 id="追加器" tabindex="-1">追加器 <a class="header-anchor" href="#追加器" aria-label="Permalink to &quot;追加器&quot;">​</a></h1><p>追加器的作用是将日志打印至指定的某处，Log4a实现了许多种追加器，其中最基础的两种分别为</p><ul><li><code>ConsoleAppender</code></li><li><code>FileAppender</code></li></ul><h2 id="consoleappender" tabindex="-1"><code>ConsoleAppender</code> <a class="header-anchor" href="#consoleappender" aria-label="Permalink to &quot;`ConsoleAppender`&quot;">​</a></h2><p>将日志输出到命令行就是通过ConsoleAppender实现的，获取Logger时，Log4a会自动分配一个ConsoleAppender至Logger,因此，正常情况下，你无需配置ConsoleAppender，除非你不希望日志在控制台上出现。</p><h2 id="禁用控制台输出" tabindex="-1">禁用控制台输出 <a class="header-anchor" href="#禁用控制台输出" aria-label="Permalink to &quot;禁用控制台输出&quot;">​</a></h2><p>要禁用控制台输出，请这么做：</p><div class="language-typescript vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 移除logger绑定的ConsoleAppender</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">removeTypedAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(AppenderTypeEnum.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">CONSOLE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div><h2 id="再次启用控制台输出" tabindex="-1">再次启用控制台输出 <a class="header-anchor" href="#再次启用控制台输出" aria-label="Permalink to &quot;再次启用控制台输出&quot;">​</a></h2><p>如果在某些情况下，你移除了ConsoleAppender，但后续又需要在控制台中打印日志，可以这么做：</p><div class="language-typescript vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 向logger添加一个ConsoleAppender</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">addConsoleAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span></code></pre></div><div class="warning custom-block github-alert"><p class="custom-block-title">WARNING</p><p>每个logger至多绑定一个ConsoleAppender，如果logger已绑定ConsoleAppender，则调用addConsoleAppender不会发生任何事。</p></div><h2 id="fileappender" tabindex="-1"><code>FileAppender</code> <a class="header-anchor" href="#fileappender" aria-label="Permalink to &quot;`FileAppender`&quot;">​</a></h2><p>FileAppender提供输出日志到文件的能力，需要开发者手动绑定至Logger。创建FileAppender时开发者可以设置日志缓存最大数量、日志文件占用上限、是否使用多线程、加密函数。</p><div class="tip custom-block github-alert"><p class="custom-block-title">TIP</p><p>对于同一个文件，FileAppender在全局下是唯一的，多个Logger可以配置相同的FileAppender，这意味着一个FileAppender可以被绑定至多个Logger实例。这是log4a内部的实现，实际开发时，开发者无需操心这一点。</p></div><p>添加一个FileAppender需要提供两个必选参数：</p><ul><li>文件路径</li><li>FileAppender名称</li></ul><div class="tip custom-block github-alert"><p class="custom-block-title">TIP</p><p>名称用于作为索引来删除某已绑定到Logger的FileAppender</p><p>从1.3.1版本开始，可以通过LogManager.setLogFilePath来指定所有日志文件的存储路径，而不是在每新建一个文件追加器时都从上下文中获取一次文件路径。但必须确保新建追加器时只提供文件名。</p></div><h2 id="添加一个fileappender" tabindex="-1">添加一个FileAppender <a class="header-anchor" href="#添加一个fileappender" aria-label="Permalink to &quot;添加一个FileAppender&quot;">​</a></h2><p>要添加一个FileAppender，可以这么做：</p><div class="language-typescript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">addFileAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">getContext</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).filesDir </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;/fileName.log&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;mainLog&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>这段代码向logger添加了一个具名FileAppender，名称为<code>mainLog</code>，指定了文件路径为应用文件沙箱路径下的<code>fileName.log</code>。</p><h2 id="添加一个多线程fileappender" tabindex="-1">添加一个多线程FileAppender <span class="VPBadge tip"><!--[-->1.3.0-rc.1 +<!--]--></span> <a class="header-anchor" href="#添加一个多线程fileappender" aria-label="Permalink to &quot;添加一个多线程FileAppender &lt;Badge type=&quot;tip&quot; text=&quot;1.3.0-rc.1 +&quot; /&gt;&quot;">​</a></h2><p>要添加具有多线程能力的FileAppender，可以通过以下方式:</p><div class="language-typescript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">addFileAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">    getContext</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).filesDir </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;/fileName.log&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">    &#39;mainLog&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    Level.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ALL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    {</span></span>
<span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">        useWorker: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="tip custom-block github-alert"><p class="custom-block-title">TIP</p><p>为了尽可能不影响应用本身的并发需要，不论开发者设置多少个多线程FileAppender，Log4a都将只占用一个线程。</p></div><h2 id="配置文件缓存" tabindex="-1">配置文件缓存 <span class="VPBadge tip"><!--[-->1.3.0-rc.1 +<!--]--></span> <a class="header-anchor" href="#配置文件缓存" aria-label="Permalink to &quot;配置文件缓存 &lt;Badge type=&quot;tip&quot; text=&quot;1.3.0-rc.1 +&quot; /&gt;&quot;">​</a></h2><p>如果需要配置日志文件最大缓存个数及最大占用，需要在options中传入相关参数</p><div class="language-typescript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">addFileAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">    getContext</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).filesDir </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;/fileName.log&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">    &#39;mainLog&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    Level.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ALL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    {</span></span>
<span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">        maxFileSize: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">        maxCacheCount: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li><code>maxFileSize</code> 指定了最大日志文件占用，单位为KB。当日志文件大小超过限制时，将会生成一个缓存文件，并新建一个日志文件以继续写入日志。</li><li><code>maxCacheCount</code> 指定了最大日志文件缓存数量，如果日志缓存数量超过该值，则会删除最早生成的日志缓存。</li></ul><h2 id="配置日志加密" tabindex="-1">配置日志加密 <a class="header-anchor" href="#配置日志加密" aria-label="Permalink to &quot;配置日志加密&quot;">​</a></h2><p>FileAppender允许开发者传入一个加密函数来进行日志加密，该函数接受两个参数，且需要返回加密后的内容：</p><ul><li>日志等级</li><li>日志内容</li></ul><p>这样的设计允许开发者只对某些或某个日志等级的日志进行加密，实现局部加密或整体加密，也使得开发者可以更加自由地编写加密算法。</p><p>配置加密：</p><div class="language-typescript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">typescript</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> encryptFunction</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">origin</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> string</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ArrayBuffer</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> string</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ArrayBuffer</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">    // 对origin利用自定义的加密算法</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">    return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> origin;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.logger.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">addFileAppender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">    getContext</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">this</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).filesDir </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;/fileName.log&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">    &#39;mainLog&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    Level.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ALL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    {</span></span>
<span class="line highlighted"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">        encryptor</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: (</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">level</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Level</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">log</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> string</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ArrayBuffer</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line highlighted"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">            if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(level.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;privateLevel&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line highlighted"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">                return</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> encryptFunction</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(log);</span></span>
<span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">            }</span></span>
<span class="line highlighted"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">            return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> log; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 如果不需要加密，必须返回原始log</span></span>
<span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">        }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-d4a0bba5><!--[--><!--]--><!----><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-d4a0bba5><span class="visually-hidden" id="doc-footer-aria-label" data-v-d4a0bba5>Pager</span><div class="pager" data-v-d4a0bba5><a class="VPLink link pager-link prev" href="/log4a-docs/guide/getting-started" data-v-d4a0bba5><!--[--><span class="desc" data-v-d4a0bba5>Previous page</span><span class="title" data-v-d4a0bba5>快速开始</span><!--]--></a></div><div class="pager" data-v-d4a0bba5><a class="VPLink link pager-link next" href="/log4a-docs/guide/parameter-priority" data-v-d4a0bba5><!--[--><span class="desc" data-v-d4a0bba5>Next page</span><span class="title" data-v-d4a0bba5>参数作用优先级</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><footer class="VPFooter has-sidebar" data-v-5d98c3a5 data-v-e315a0ad><div class="container" data-v-e315a0ad><p class="message" data-v-e315a0ad>以 Apache License 2.0 许可证发布</p><p class="copyright" data-v-e315a0ad>Copyright ©️ 2024-至今 郭挺劲</p></div></footer><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"en_index.md\":\"BF7jtfOe\",\"guide_format.md\":\"C79OSdvL\",\"reference_log-manager.md\":\"BeIm4LSX\",\"guide_intercept-console.md\":\"FcFJxN1c\",\"reference_daily-rolling-file-appender.md\":\"1zqeKUct\",\"reference_abstract-appender.md\":\"Cem4bulA\",\"zh-tw_reference_tags.md\":\"DqxMdvls\",\"reference_file-appender.md\":\"BIVgBS12\",\"en_reference_log-manager.md\":\"CbgPELgf\",\"guide_custom-layout.md\":\"C0TuAlRA\",\"guide_what-is-log4a.md\":\"CK61PeoU\",\"reference_decorators.md\":\"CqFbgAHb\",\"guide_parameter-priority.md\":\"I1gaByrU\",\"reference_tags.md\":\"5FtazP79\",\"guide_getting-started.md\":\"CbXTYjw6\",\"reference_tcp-socket-appender.md\":\"D_tlKq61\",\"reference_udp-socket-appender.md\":\"dF2yC5YV\",\"guide_appender.md\":\"lJX8Kh1o\",\"zh-tw_guide_what-is-log4a.md\":\"CsLr107O\",\"en_reference_tags.md\":\"BarVvWZS\",\"zh-tw_reference_decorators.md\":\"tvSSaDDU\",\"guide_custom-appender.md\":\"DImclcsg\",\"zh-tw_guide_getting-started.md\":\"CEE4AIWU\",\"zh-tw_guide_appender.md\":\"C8H2zUM7\",\"zh-tw_index.md\":\"B6t1k6p3\",\"index.md\":\"U3nfVLpR\",\"en_guide_what-is-log4a.md\":\"-57KX4bx\",\"guide_rolling-file-appender.md\":\"DPAbW_Js\",\"en_reference_logger.md\":\"BUHfLhiT\",\"en_reference_marker-manager.md\":\"Cospl-DS\",\"reference_logger.md\":\"yqhQwVzU\",\"guide_log-to-socket.md\":\"KSzJqgFG\",\"reference_marker-manager.md\":\"T-lmE7KI\",\"zh-tw_reference_marker-manager.md\":\"DmivIgcY\",\"zh-tw_reference_logger.md\":\"CpImrc99\",\"en_reference_decorators.md\":\"DYFu_3lp\",\"zh-tw_reference_log-manager.md\":\"D9IIjYl7\",\"guide_log-with-worker.md\":\"CGFZ9AYZ\",\"en_guide_appender.md\":\"CcF2MM9v\",\"en_guide_getting-started.md\":\"DjONPgfJ\",\"guide_best-practice.md\":\"4fkbkJ2Q\",\"reference_level.md\":\"Dd9l-xdt\",\"reference_rolling-file-appender.md\":\"Bht_8rK4\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"Log4a\",\"description\":\"优雅、易用的HarmonyOS日志库\",\"base\":\"/log4a-docs/\",\"head\":[],\"router\":{\"prefetchLinks\":true},\"appearance\":true,\"themeConfig\":{\"nav\":[{\"text\":\"指南\",\"link\":\"/guide/getting-started\"},{\"text\":\"参考\",\"link\":\"/reference/log-manager\"},{\"text\":\"1.4.0\",\"items\":[{\"text\":\"更新日志\",\"link\":\"https://gitee.com/ericple/log4a/blob/master/library/CHANGELOG.md\"}]}],\"sidebar\":{\"/guide/\":[{\"text\":\"简介\",\"items\":[{\"text\":\"什么是Log4a?\",\"link\":\"/guide/what-is-log4a\"},{\"text\":\"快速开始\",\"link\":\"/guide/getting-started\"},{\"text\":\"追加器\",\"link\":\"/guide/appender\"}],\"collapsed\":false},{\"text\":\"输出日志\",\"items\":[{\"text\":\"参数作用优先级\",\"link\":\"/guide/parameter-priority\"},{\"text\":\"使用多线程输出\",\"link\":\"/guide/log-with-worker\"},{\"text\":\"日志消息构建\",\"link\":\"/guide/format\"}]},{\"text\":\"进阶使用\",\"items\":[{\"text\":\"自定义追加器\",\"link\":\"/guide/custom-appender\"},{\"text\":\"输出到服务器\",\"link\":\"/guide/log-to-socket\"},{\"text\":\"自定义布局\",\"link\":\"/guide/custom-layout\"},{\"text\":\"console拦截\",\"link\":\"/guide/intercept-console\"},{\"text\":\"最佳实践\",\"link\":\"/guide/best-practice\"}]},{\"text\":\"API参考\",\"link\":\"/reference/log-manager\"}],\"/reference/\":[{\"text\":\"API参考\",\"items\":[{\"text\":\"LogManager\",\"link\":\"/reference/log-manager\"},{\"text\":\"Logger\",\"link\":\"/reference/logger\"},{\"text\":\"MarkerManager\",\"link\":\"/reference/marker-manager\"},{\"text\":\"Level\",\"link\":\"/reference/level\"},{\"text\":\"追加器\",\"items\":[{\"text\":\"AbstractAppender\",\"link\":\"/reference/abstract-appender\"},{\"text\":\"FileAppender\",\"link\":\"/reference/file-appender\"},{\"text\":\"RollingFileAppender\",\"link\":\"/reference/rolling-file-appender\"},{\"text\":\"DailyRollingFileAppender\",\"link\":\"/reference/daily-rolling-file-appender\"},{\"text\":\"TCPSocketAppender\",\"link\":\"/reference/tcp-socket-appender\"},{\"text\":\"UDPSocketAppender\",\"link\":\"/reference/udp-socket-appender\"}]}],\"collapsed\":false},{\"text\":\"装饰器和标签\",\"items\":[{\"text\":\"装饰器\",\"link\":\"/reference/decorators\"},{\"text\":\"标签\",\"link\":\"/reference/tags\",\"collapsed\":false}],\"collapsed\":false}]},\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/ericple/log4a\"}],\"footer\":{\"message\":\"以 Apache License 2.0 许可证发布\",\"copyright\":\"Copyright ©️ 2024-至今 郭挺劲\"},\"search\":{\"provider\":\"local\"},\"logo\":{\"src\":\"/log4a@1x.svg\"}},\"locales\":{\"root\":{\"label\":\"简体中文\",\"lang\":\"zh\"}},\"scrollOffset\":134,\"cleanUrls\":true}");</script>
    
  </body>
</html>